#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@author:atuo.cn
'''

from tkinter import *
from tkinter.ttk import *
from tkinter.simpledialog import Dialog
from functools import partial

from dialogs.dialoghelper import DialogHelper

class HintsDialogLayout(Dialog, DialogHelper):
    def __init__(self, master, wmtitle):
        Dialog.__init__(self, master, wmtitle)

    #override method of Dialog to create main frame
    def body(self, master):
        self.opts={
            "IgnoreEmptySection":IntVar(),
            "FilterKeywords":IntVar(),
            "FilterKeywords_String":StringVar(),
            "Prefix":IntVar(),
            "Prefix_String":StringVar(),
        }
        
        frame=LabelFrame(master, text="定位章节标题选项")
        frame.pack()
        check=Checkbutton(frame, variable=self.opts["IgnoreEmptySection"], text="忽略空白章节")
        check.grid(row=0,column=0,sticky=W,pady=5)
        check=Checkbutton(frame, 
                          variable=self.opts["FilterKeywords"],
                          text="过滤关键字")
        check.grid(row=1,column=0,sticky=W,pady=5)
        #lambda cann't make a closure because of the check variable in the same scope.
        #Use partial function instead.
        self.opts["FilterKeywords"].trace_variable(mode="w", callback=partial(self.oncheck,check))
        entry=Entry(frame, width=30, textvariable=self.opts["FilterKeywords_String"])
        entry.grid(row=1,column=1,sticky=W,pady=5)
        check=Checkbutton(frame, 
                          variable=self.opts["Prefix"],
                          text="标题前缀")
        check.grid(row=2,column=0,sticky=W,pady=5)
        self.opts["Prefix"].trace_variable(mode="w", callback=partial(self.oncheck,check))
        entry=Entry(frame, width=30, textvariable=self.opts["Prefix_String"])
        entry.grid(row=2,column=1,sticky=W,pady=5)

        self._bindEvents()
        self.onLoad()
        self.update_idletasks()
                        
    def _bindEvents(self):
        pass
        
    def onLoad(self):
        pass

    def oncheck(self, wdgt,name, index, mode):
        info=wdgt.grid_info()
        wdgt_list=wdgt.master.grid_slaves(info["row"], info["column"]+1)
        checked=getboolean(wdgt.getvar(name=name))
        for w in wdgt_list:
            st ="!readonly" if checked else "readonly"
            w.state([st])
        
